extends BaseStage

@export var current_enemy:Enemy


const HalfMusic := preload("res://assets/music/Stage3 4-5.mp3")

func _ready() -> void:
	fake_bg.visible = true
	Global.player.global_position = spawn_point.global_position
	current_npc.change_emotion(start_emotion)
	DialogHandler.dialogue_end.connect(on_dialogue_end.unbind(1))
	GameEvents.stage_entered.emit()
	SaveManager.save_game()
	if MusicPlayer.bgm_player.stream != HalfMusic:
		MusicPlayer.play_bgm(stage_bgm)
	$CheckPoints/CheckPoint3.body_entered.connect(on_chackpoint_entered.unbind(1))
	$CheckPoints/CheckPoint4.body_entered.connect(func(_body): 
		current_enemy.die()
		current_npc.change_emotion(Npc.Emotion.MANIA)
		MusicPlayer.play_bgm(HalfMusic)
	)
	if Global.player.global_position == spawn_point.global_position:
		Global.player.is_dead = false

func on_dialogue_end() -> void:
	super.on_dialogue_end()
	if current_count == 1:
		current_enemy.start()
	elif current_count == 2:
		if $StaticBody2D3 != null:
			$StaticBody2D3.queue_free()
		


func delete_dialogue_area(count: int) -> void:
	super.delete_dialogue_area(count)
	$StaticBody2D3.queue_free()
	current_enemy.start()

func on_chackpoint_entered() -> void:
	current_npc.change_emotion(Npc.Emotion.MANIA)
	MusicPlayer.play_bgm(HalfMusic)

#func _on_check_point_body_entered(_body: Node2D) -> void:
	#current_enemy.set_ai_mode(0)
